<template>
  <el-table :data="ListArr" style="width: 100%">
    <el-table-column prop="id" label="ID" />
    <el-table-column prop="title" label="标题" />
    <!-- <el-table-column prop="is_select" label="公开状态" /> -->
    <el-table-column
      prop="is_select"
      label="公开状态"
      :formatter="stateFormat" />
    <el-table-column prop="content" label="内容" />
    <el-table-column fixed="right" label="操作" min-width="120">
      <template #default="User">
        <el-button size="small" type="primary" @click="ChangeFn(User.row)"
          >编辑</el-button
        >
        <el-button size="small" type="danger" @click="DeleteFn(User.row)"
          >删除</el-button
        >
      </template>
    </el-table-column>
  </el-table>
  <!-- 编辑弹出 -->
  <el-dialog v-model="dialogFormVisible" width="300" center>
    <el-form :model="form">
      <el-form-item label="标题" label-width="100px">
        <el-input v-model="form.title" autocomplete="off" />
      </el-form-item>
      <el-form-item label="公开状态" label-width="100px">
        <el-checkbox v-model="form.is_select" size="large" />
      </el-form-item>
      <el-form-item label="内容" label-width="100px">
        <el-input v-model="form.content" autocomplete="off" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">重置</el-button>
        <el-button type="primary" @click="xiugaiFn">修改</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import http from "@/utils/http";
import { ref, reactive, computed } from "vue";
// import { useRouter } from "vue-router";
const ListArr = ref([]);
const dialogFormVisible = ref(false);
let id = "";

const form = reactive({
  title: "",
  is_select: false,
  content: "",
});

function stateFormat(row) {
  // console.log(row);
  // console.log(row.status);
  if (row.status === true) {
    return "是";
  } else {
    return "否";
  }
}
function ChangeFn(item) {
  // console.log(item.id);
  id = item.id;
  // console.log(id);
  dialogFormVisible.value = true;
  form.title = item.title;
  form.content = item.content;
  form.is_select = item.status;
}
function loadList() {
  http
    .request({
      url: "/posts",
      method: "get",
    })
    .then((res) => {
      console.log(res.data);
      ListArr.value = res.data;
    });
}
loadList();
// 编辑用户信息
function xiugaiFn(item) {
  http
    .request({
      url: "/posts/" + id,
      method: "patch",
      data: {
        title: form.title,
        content: form.content,
        status: form.is_select,
      },
    })
    .then((res) => {
      console.log(res.data);
      loadList();
      dialogFormVisible.value = false;
    });
}
function DeleteFn(item) {
  console.log(item);
  http
    .request({
      url: "/posts/" + item.id,
      method: "delete",
      data: {
        id: item.id,
      },
    })
    .then((res) => {
      console.log(res.data);
      loadList();
    });
}
</script>
<style lang="css" scoped></style>
